//
//  Show_Button_ViewController.m
//  WWAVPlayer
//
//  Created by dllo on 15/10/30.
//  Copyright © 2015年 LZY. All rights reserved.
//

#import "Show_Button_ViewController.h"
#import "ShowAllCell.h"
#import "ShowAllMoel.h"
#import <UIImageView+WebCache.h>
#import <MJRefresh.h>
@interface Show_Button_ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *arrOfModel;
@end

@implementation Show_Button_ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
  
    [self handleData];
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor grayColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];

    [self addHeader];
    [self loadMoreData];
  
}


- (void)handleData
{
    
    NSString *str = [NSString stringWithFormat:@"http://pv.funshion.com/v5/video/retrieval?cate=0&channel=%ld&cl=iphone&order=pi&pg=1&si=0&uc=36&ve=3.1.1.4",self.channelNum];
    [NetWorkManager GET:str isCaChe:YES resultData:^(id responseObject) {
       
        NSDictionary *dic = responseObject;
        NSMutableArray *arr = [NSMutableArray arrayWithArray:[dic objectForKey:@"videos"]];
        self.arrOfModel = [NSMutableArray array];
        for (NSDictionary *dic in arr) {
            ShowAllMoel *show = [[ShowAllMoel alloc]init];
            [show setValuesForKeysWithDictionary:dic];
            [self.arrOfModel addObject:show];
        }
        
        [self.tableView reloadData];
        
    } resultRrror:^(NSError *error) {
        
    }];
    
}


//下拉刷新
- (void)addHeader
{
    //下拉刷新
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //延迟2秒
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 刷新表格
            [self handleData];
            [self.tableView reloadData];
            [self.tableView.header endRefreshing];
        });
    }];
    
    //设置刷新控件
    self.tableView.header = header;
}
//上拉加载
- (void)loadMoreData
{
    __block  int i = 2;
    self.tableView.footer = [MJRefreshAutoGifFooter footerWithRefreshingBlock:^{
        NSString *str = [NSString stringWithFormat:@"http://pv.funshion.com/v5/video/retrieval?cate=0&channel=%ld&cl=iphone&order=pi&pg=%d&si=0&uc=36&ve=3.1.1.4",self.channelNum,i];
        i ++ ;
        [NetWorkManager GET:str isCaChe:YES resultData:^(id responseObject) {
            
            NSDictionary *dic = responseObject;
            NSMutableArray *arr = [NSMutableArray arrayWithArray:[dic objectForKey:@"videos"]];
      
            NSMutableArray *arr2 = [NSMutableArray array];
            for (NSDictionary *dic in arr) {
                ShowAllMoel *show = [[ShowAllMoel alloc]init];
                [show setValuesForKeysWithDictionary:dic];
                [arr2 addObject:show];
            }
            self.arrOfModel = (NSMutableArray *)[self.arrOfModel arrayByAddingObjectsFromArray:arr2];
             [self.tableView reloadData];
            [self.tableView.footer endRefreshing];
            if (arr2.count == 0) {
          [self.tableView.footer endRefreshingWithNoMoreData];
               
            }
           
        } resultRrror:^(NSError *error) {
            
        }];

    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrOfModel.count;
    
}
//重用池
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuseID = @"reuse2";
    ShowAllCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
    if (cell == nil) {
        cell = [[ShowAllCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseID];
        
    }
    
    ShowAllMoel *show = [self.arrOfModel objectAtIndex:indexPath.row];
    [cell.myimageView8 sd_setImageWithURL:[NSURL URLWithString:show.still]];
    cell.myLabel1.text = show.name;
    cell.myLabel2.text = [NSString stringWithFormat:@"播放 %@ | %@",show.vv,show.releasePZ];
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110 * H;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
